function EventEmitter() {
  this._events = {};
}
EventEmitter.prototype.on = function (eventName, callback) {
  if (!this._events) this._events = {};
  // 先执行newListener 在执行放到数组的操作
  if (eventName !== "newListener") {
    // 可以让用户监听某个时间，来监控后续的操作.
    this.emit("newListener", eventName);
  }
  (this._events[eventName] || (this._events[eventName] = [])).push(callback);
};
EventEmitter.prototype.once = function (eventName, callback) {
  let once = (...args) => {
    callback(...args); // 原始要执行的函数
    this.off(eventName, once);
  };
  once.l = callback; // 自定义属性
  this.on(eventName, once);
};
EventEmitter.prototype.emit = function (eventName, ...args) {
  if (!this._events) this._events = {};
  let listeners = this._events[eventName];
  if (listeners) {
    listeners.forEach((fn) => {
      fn(...args);
    });
  }
};
EventEmitter.prototype.off = function (eventName, callback) {
  if (!this._events) this._events = {};
  let listeners = this._events[eventName]; // 移除掉需要关闭的函数
  if (listeners) {
    this._events[eventName] = listeners.filter(
      (item) => item !== callback && item.l !== callback
    );
  }
};
module.exports = EventEmitter;
